﻿using CSRedis;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Hosting;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using ZR.Admin.WebApi.Extensions;
using ZR.Model.mes.md;
using ZR.Model.mes.md.DTO;
using ZR.Service.mes.md;
using ZR.Service.mes.md.IService;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace ZR.Admin.WebApi.Controllers.mes.md
{
    [Route("mes/md/product")]
    public class MdProductDefineController : BaseController
    {
        private readonly IMdProductDefineService mdProduct;
        public MdProductDefineController(IMdProductDefineService mdProduct)
        {
            this.mdProduct = mdProduct;
        }
        /// <summary>
        /// 分页查寻 unti
        /// </summary>
        /// <param name="pageNum">页码</param>
        /// <param name="pagesize">页尺</param>
        /// <param name="name">单位名称</param>
        /// <param name="code">单位代码</param>
        /// <returns></returns>
        [HttpGet("list")]
        public IActionResult Getlist(int pageNum, int pagesize, string name = "", string code = "")
        {
            var unitPageDto = mdProduct.GetList(name, code, pageNum, pagesize);
            return SUCCESS(unitPageDto);
        }

        /// <summary>
        /// 根据单位名称查询单位
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>

        [HttpGet("getUnit/{name}")]
        public IActionResult GetUnitlist(string name)
        {
            List<MdUnit> units = mdProduct.GetProductDefineList(name);
            return SUCCESS(units);
        }

        /// <summary>
        /// 获取全部单位
        /// </summary>
        /// <returns></returns>
        [HttpGet("getUnit")]
        public IActionResult GetUnitlist()
        {
            List<MdUnit> units = mdProduct.GetProductDefineList();
            return SUCCESS(units);
        }
        /// <summary>
        /// 获取全部 产线
        /// </summary>
        /// <returns></returns>
        [HttpGet("getProcessroute")]
        public IActionResult GetProcessroutelist()
        {
            List<MdWorkline> worklines = mdProduct.GetWorklineList();
            return SUCCESS(worklines);
        }


        /// <summary>
        /// add 产品
        /// </summary>
        /// <param name="products"></param>
        /// <returns></returns>
        [HttpPost("addProductDefine")]
        public IActionResult addProductDefine([FromBody] MdProductDefine products)
        {
            if (products == null)
            {
                return ToResponse(new ApiResult((int)ResultCode.NO_DATA, "null"));
            }
            if (products != null)
            {
                products.ToCreate(HttpContext);
            }
            int result = mdProduct.InsertProductDefine(products);
            return SUCCESS(result);
        }


        /// <summary>
        /// 根据主键查询product
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("getProductBypk/{id}")]
        public IActionResult GetProductBypk(int id)
        {
            MdProductDefine mdProductDefine = mdProduct.GetProductDefinebyPK(id);
            return SUCCESS(mdProductDefine);
        }


        /// <summary>
        /// 修改产品
        /// </summary>
        /// <param name="products"></param>
        /// <returns></returns>
        [HttpPost("updateProductDefine")]
        public IActionResult UpdateProductDefine([FromBody] MdProductDefine products)
        {
            if (products != null)
                products.ToUpdate(HttpContext);

            int result = mdProduct.UpdateProductDefine(products);
            return SUCCESS(result);


        }

        /// <summary>
        /// 删除产品
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>

        [HttpDelete("delproduct/{id}")]
        public IActionResult DelUnit(string id)
        {
            int[] ids = Tools.SpitIntArrary(id);
            int result = mdProduct.deleteProductDefine(ids);
            return ToResponse(result);
        }


        /// <summary>
        /// 导出产品excel
        /// </summary>
        /// <param name="starttime"></param>
        /// <param name="endtime"></param>
        /// <param name="productCode"></param>
        /// <param name="productName"></param>
        /// <returns></returns>
        [HttpGet("exportProduct")]
        public IActionResult ExportProduct(DateTime starttime, DateTime endtime, string productCode = "", string productName = "")
        {
            List<MdProductDefine> units = null;

            units = mdProduct.GetList(starttime, endtime, productCode, productName);


            var result = ExportExcelMini(units, "pruduct", "产品列表");
            return ExportExcel(result.Item2, result.Item1);

        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="updateSupport">0：只新增 1：新增且更新</param>
        /// <returns></returns>
        [HttpPost("importExcel")]
        public IActionResult ImportExcel()
        {


            IFormFileCollection files = Request.Form.Files;
            foreach (IFormFile file in files)
            {
                string directory = Path.Combine(App.WebHostEnvironment.ContentRootPath, "Images");
                string fileExtension = Path.GetExtension(file.FileName);
                string fileName = Guid.NewGuid().ToString() + fileExtension;
                string filePath = Path.Combine(directory, fileName);// 生成文件路径

                if (!Directory.Exists(directory))
                {
                    Directory.CreateDirectory(directory);
                }

                // 写入文件
                using (var stream = new FileStream(filePath, FileMode.Create))
                {
                    file.CopyTo(stream);
                    stream.Flush();
                }
            }
            return null;
        }

        public IActionResult importTemplate()
        {
            return null;

        }
    }

}
